#include "head.h"
#include "func.h"
int get_filesize(char *filename){
    struct stat st;
    if(stat(filename,&st)==-1){
        return 0;//文件不存在
    }
    return st.st_size;
}
int recv_file_train(int fd_socket,char *filename){
    off_t offset=get_filesize(filename);
    send(fd_socket,&offset,sizeof(off_t),0);
    int fd=open(filename,O_RDWR|O_CREAT,0666);
    if(offset>0){
        lseek(fd,offset,SEEK_SET);
    }
    train_t train;
    while(1){
        bzero(&train,sizeof(train._data));
        /* ssize_t sret1=recv(fd_socket,&train._data_len,sizeof(train._data_len),MSG_WAITALL); */
        recv(fd_socket,&train._data_len,sizeof(train._data_len),MSG_WAITALL);
        if(train._data_len==0){
            printf("upload success!\n");
            close(fd);
            return 0;
        }
        ssize_t sret2=recv(fd_socket,train._data,train._data_len,MSG_WAITALL);
        if(sret2==0){
            printf("Disconnected.\n");
        }
        write(fd,train._data,train._data_len);
    }
    close(fd);
    return 0;
}

